<html>
<head>
<title>AddAndEditDialog.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(0,0,255); }
.s3 { color: rgb(0,128,0); font-weight: bold; }
.s4 { color: rgb(128,128,128); font-style: italic; }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
AddAndEditDialog.java</FONT>
</center></TD></TR></TABLE>
<pre>

<span class="s0">package </span><span class="s1">com.example.lista_zakupow.dialogs; 
 
</span><span class="s0">import </span><span class="s1">android.app.Dialog; 
</span><span class="s0">import </span><span class="s1">android.content.Context; 
</span><span class="s0">import </span><span class="s1">android.view.View; 
</span><span class="s0">import </span><span class="s1">android.widget.Button; 
</span><span class="s0">import </span><span class="s1">android.widget.EditText; 
</span><span class="s0">import </span><span class="s1">android.widget.Toast; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.R; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.adapters.SpecialAdapter; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.database.dao.ListaZakupowDao; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.zakupy.ListaZakupow; 
 
</span><span class="s0">public class </span><span class="s1">AddAndEditDialog </span><span class="s0">extends </span><span class="s1">Dialog { 
    </span><span class="s0">private static final int </span><span class="s1">EDIT = </span><span class="s2">1</span><span class="s1">; 
    </span><span class="s0">private static final int </span><span class="s1">ADD = </span><span class="s2">2</span><span class="s1">; 
    </span><span class="s0">private int </span><span class="s1">chose; 
    </span><span class="s0">private </span><span class="s1">ListaZakupow selectedItem; 
 
    </span><span class="s0">private </span><span class="s1">ListaZakupowDao listaZakupowDao; 
 
    </span><span class="s0">private </span><span class="s1">EditText listNameEitText; 
 
    </span><span class="s0">private </span><span class="s1">SpecialAdapter&lt;ListaZakupow&gt; adapter; 
 
    </span><span class="s0">public </span><span class="s1">AddAndEditDialog(Context context, SpecialAdapter&lt;ListaZakupow&gt; adapter, </span><span class="s0">boolean </span><span class="s1">isEdit, ListaZakupow selectedItem) { 
        </span><span class="s0">super</span><span class="s1">(context); 
        setContentView(R.layout.add_and_edit_list); 
 
        </span><span class="s0">this</span><span class="s1">.selectedItem = selectedItem; 
 
        listaZakupowDao = </span><span class="s0">new </span><span class="s1">ListaZakupowDao(context); 
 
        </span><span class="s0">this</span><span class="s1">.adapter = adapter; 
 
        listNameEitText = (EditText) findViewById(R.id.listNameEditText); 
 
        Button buttonSave = (Button) findViewById(R.id.buttonSave); 
        buttonSave.setOnClickListener(</span><span class="s0">new </span><span class="s1">ButtonSaveClickListener()); 
 
        </span><span class="s0">if </span><span class="s1">(isEdit) chose = EDIT; 
        </span><span class="s0">else </span><span class="s1">chose = ADD; 
 
        prepareDialog(); 
    } 
 
    </span><span class="s0">private void </span><span class="s1">prepareDialog() { 
        </span><span class="s0">if </span><span class="s1">(chose == EDIT) { 
            listNameEitText.setText(selectedItem.getName()); 
            Toast.makeText(getContext(), selectedItem.getName(), Toast.LENGTH_LONG).show(); 
        } 
    } 
 
    @Override 
    </span><span class="s0">protected void </span><span class="s1">onStop() { 
        </span><span class="s0">super</span><span class="s1">.onStop(); 
        listNameEitText.setText(</span><span class="s3">&quot;&quot;</span><span class="s1">); 
    } 
 
    </span><span class="s0">private class </span><span class="s1">ButtonSaveClickListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener { 
        @Override 
        </span><span class="s0">public void </span><span class="s1">onClick(View view) { 
            </span><span class="s0">switch </span><span class="s1">(chose) { 
                </span><span class="s0">case </span><span class="s1">ADD: 
                    ListaZakupow listaZakupow = </span><span class="s0">new </span><span class="s1">ListaZakupow(listNameEitText.getText().toString()); 
                    listaZakupowDao.save(listaZakupow); 
                    adapter.addItem(listaZakupow); 
                    listNameEitText.setText(</span><span class="s3">&quot;&quot;</span><span class="s1">); 
                    dismiss(); 
                </span><span class="s0">case </span><span class="s1">EDIT: 
                    </span><span class="s4">// kod edit</span><span class="s1"> 
                    </span><span class="s0">break</span><span class="s1">; 
            } 
        } 
    } 
} 
</span></pre>
</body>
</html>